<?php
/**
 * Scd Framework
 *
 * LICENSE
 *
 * This source file is subject to the BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.njwz.cn/license/
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to Eric <scd113@gmail.com> so we can send you a copy immediately.
 *
 * @category   Scd
 * @package    Scd
 * @copyright  Copyright (c) 2007 Blue Web Studio (http://www.njwz.cn)
 * @license    http://framework.njwz.cn/license/     
 * @version    $Id: Scd.php 2007-01-13 02:12:49Z eric $
 */

class Scd_Exception
{
	
    /**
     * 异常消息
     *
     * @var string
     */
    var $msg = 'Unknown exception';

    /**
     * 错误代码
     */
    var $code = 0;

    /**
     * 抛出异常的文件
     *
     * @var string
     */
    var $file;

    /**
     * 抛出异常代码的行号
     *
     * @var int
     */
    var $line;

    /**
     * 调用堆栈
     *
     * @var array
     */
    var $trac;
    
	function Scd_Exception($msg = null, $code = 0, $out = 'true')
	{
        $this->msg = $msg;
        $this->code = $code;
        $this->trac = debug_backtrace();

        // 取得抛出异常的文件和代码行号
        $last = array_shift($this->trac);
        $this->file = $last['file'];
        $this->line = $last['line'];
        
        //是否输出
        if ($out) {
        	echo $this->__toString();
//        	exit;
        }
	}
	
    /**
     * 获得异常错误信息
     *
     * @return string
     */
    function getMessage() {
        return $this->message;
    }

    /**
     * 获得异常错误代码
     *
     * @return int
     */
    function getCode() {
        return $this->code;
    }

    /**
     * 获得抛出异常的文件名
     *
     * @return string
     */
    function getFile() {
        return $this->file;
    }

    /**
     * 获得抛出异常的代码行号
     *
     * @return int
     */
    function getLine() {
        return $this->line;
    }

    /**
     * 返回调用堆栈
     *
     * @return array
     */
    function getTrace() {
        return $this->trac;
    }

    /**
     * 返回字符串表示的调用堆栈
     */
    function getTraceAsString() {
        $out = '';
        $ix = 0;
        foreach ($this->trac as $point) {
            $out .= "#{$ix} {$point['file']}({$point['line']}): {$point['function']}(";
            if (is_array($point['args']) && count($point['args']) > 0) {
                foreach ($point['args'] as $arg) {
                    switch (gettype($arg)) {
                    case 'array':
                    case 'resource':
                        $out .= gettype($arg);
                        break;
                    case 'object':
                        $out .= get_class($arg);
                        break;
                    case 'string':
                        /*if (strlen($arg) > 30) {
                            $arg = substr($arg, 0, 30) . ' ...';
                        }
                        */
                        $out .= "'{$arg}'";
                        break;
                    default:
                        $out .= $arg;
                    }
                    $out .= ', ';
                }
                $out = substr($out, 0, -2);
            }
            $out .= ")\n";
            $ix++;
        }
        $out .= "#{$ix} {main}\n";

        return $out;
    }

    /**
     * 返回异常的字符串形式
     *
     * @return string
     */
    function __toString() {
        $out = "<pre>exception <b>'" . get_class($this) . "'</b>";
        if ($this->msg != '') {
            $out .= " with message <b>'{$this->msg}'</b>";
        }
        $out .= " in <b>{$this->file}</b>:<u>{$this->line}</u>\n\n";
        $out .= $this->getTraceAsString();
        $out .= "</pre>";
        return $out;
    }
}
?>